package com.lagou.rocket.demo.listener;

import com.lagou.rocket.demo.constants.TopicName;
import com.lagou.rocket.demo.po.Order;
import com.lagou.rocket.demo.service.OrderService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        topic = TopicName.CANCEL_ORDER_TOPIC, // 当前消费者消费的主题名称
        consumerGroup = "cancel_order_grp" // 当前消费者的消费组名称
)
public class CancelOrderListener implements RocketMQListener<String> {

    @Autowired
    private OrderService orderService;

    @Override
    public void onMessage(String s) {
        // 此消息是从延迟队列取出来的，此时应该取消订单了

        Order order = new Order();
        // s就是从延迟队列取出的消息，订单号
        order.setOrderNo(s);

        final boolean flag = orderService.cancelOrder(order);
        if (flag)
            System.out.println("订单：" + s + "\t" + "已取消");

    }
}
